<template>
  <div>
    <el-tabs v-model="editableTabsValue" type="card" closable @tab-remove="removeTab">
      <el-tab-pane
        v-for="(item, index) in editableTabs"
        :key="item.name"
        :label="item.title"
        :name="item.name">
        <!-- <el-button @click="testBtn" type="primary">主要按钮</el-button> -->
        <div>
            <router-link v-bind:to="item.content">测试路由</router-link>
            <keep-alive>
                <router-view></router-view>
            </keep-alive>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style type="text/css" media="screen">
  .el-tabs__item {
    height: 30px;
    line-height: 30px;
  }
  .el-tabs__nav {
    height: 30px;
    line-height: 30px;
  }
  .el-tab-pane{
    overflow: auto;
  }
</style>

<script>

  import {mapGetters, mapActions} from 'vuex';
  import axios from 'axios';

  export default {
    computed: mapGetters({
        editableTabsValue: 'editableTabsValue',
        editableTabs: 'editableTabs'
    }),
    methods: {
      ...mapActions([
        'setEditableTabsValue',
        'setEditableTabs'
      ]),
      testBtn(){
          console.log("hello test");
          axios.get('/operation/merchandise/msku/list?currentPage=1&pageSize=10').then(response => {
            console.log('请求成功：',response);
          }).catch(errorResponse => {
            console.log('请求失败：',errorResponse);
          });
      },
      removeTab(targetName) {

        let tabs = this.editableTabs;
        let activeName = this.editableTabsValue;

        if (activeName === targetName) {
          tabs.forEach((tab, index) => {
            if (tab.name === targetName) {
              let nextTab = tabs[index + 1] || tabs[index - 1];
              if (nextTab) {
                activeName = nextTab.name;
              }
            }
          });
        }

        let letfTabs = tabs.filter(tab => tab.name !== targetName);
        this.$store.dispatch('setEditableTabsValue',activeName);
        this.$store.dispatch('setEditableTabs',letfTabs);
      },
    }
  }
</script>

